package cj.web.tb.controllers;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.annotation.rest.Post;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import cj.web.admin.constants.Constants;
import cj.web.login.controllers.BaseController;
import cj.web.tb.constants.StatusEnum;
import cj.web.tb.dao.GameDAO;
import cj.web.tb.dao.GrageDAO;
import cj.web.tb.dao.TBRecommendationDAO;
import cj.web.tb.dao.TBRecommendationItemDAO;
import cj.web.tb.dao.TBSchoolRegistrationDAO;
import cj.web.tb.domain.TBRecommendation;
import cj.web.tb.domain.TBRecommendationItem;
import cj.web.tb.domain.TBSchoolRegistration;

@Path("/tBRecommendation")
public class TBRecommendationController extends BaseController{
	
	private final static String MSG_1="状态不是审核通过和申报不通过！";
	private final static String MSG_2="申报人数过多！请查看人数！";
	private final static String MSG_3="状态不是审核通过！不能申报！";
	private final static String STATUS="待提交申报";
	
	private static Logger logger = Logger.getLogger(TBRecommendationController.class.getName());
	
	@Autowired
	private TBRecommendationDAO tBRecommendationDao;
	@Autowired
	private TBRecommendationItemDAO tBRecommendationItemDAO;
	@Autowired
	private TBSchoolRegistrationDAO tBSchoolRegistrationDao;
	@Autowired
	private GameDAO gameDao;
	@Autowired
	private GrageDAO grageDao;
	
	@Get("show")
	public String show(Model model,TBRecommendation tBRecommendation) {
		List<Map> gamelis =  gameDao.queryAllForEndDate();
		model.add("gamelis", gamelis);
		return "/views/tb/recommendationList.vm";
	}

	@Post("getByUserIdList")
	public String queryByUserIdList(TBRecommendation tBRecommendation,HttpServletRequest request) {
		//当前页  
		tBRecommendation.setStart((tBRecommendation.getPage()-1)*tBRecommendation.getRows());
		tBRecommendation.setLength(tBRecommendation.getRows());
		
		JSONObject jo = new JSONObject();
		tBRecommendation.setUserId(returnSessionObj(request).getId());
		List<TBRecommendation> objs = tBRecommendationDao.queryByUserId(tBRecommendation);
		
		for(TBRecommendation obj:objs){
			
			TBRecommendationItem tBRecommendationItem = new TBRecommendationItem();
			tBRecommendationItem.setRecommendationId(obj.getId());
			int applicationsNum = tBRecommendationItemDAO.querySize(tBRecommendationItem);
			//已申请数量
			obj.setApplicationsNum(applicationsNum);
			//状态
			if(StatusEnum.TYPE2.getValue()==obj.getStatus()){
				obj.setStatusName(STATUS);
			}else{
				obj.setStatusName(StatusEnum.getName(obj.getStatus()));
			}
		}
		
		int size = tBRecommendationDao.queryByUserIdSize(tBRecommendation);
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Post("getList")
	public String queryList(TBRecommendation tBRecommendation) {
		//当前页  
		tBRecommendation.setStart((tBRecommendation.getPage()-1)*tBRecommendation.getRows());
		tBRecommendation.setLength(tBRecommendation.getRows());
		
		JSONObject jo = new JSONObject();
		
		List<TBRecommendation> objs = tBRecommendationDao.query(tBRecommendation);
		
		for(TBRecommendation obj:objs){
			
			TBRecommendationItem tBRecommendationItem = new TBRecommendationItem();
			tBRecommendationItem.setRecommendationId(obj.getId());
			int applicationsNum = tBRecommendationItemDAO.querySize(tBRecommendationItem);
			//已申请数量
			obj.setApplicationsNum(applicationsNum);
			//状态
			if(StatusEnum.TYPE2.getValue()==obj.getStatus()){
				obj.setStatusName(STATUS);
			}else{
				obj.setStatusName(StatusEnum.getName(obj.getStatus()));
			}
		}
		
		int size = tBRecommendationDao.querySize(tBRecommendation);
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Get("createPage")
	public String createPage(Model model,HttpServletRequest request) {
		
		TBSchoolRegistration obj = tBSchoolRegistrationDao.queryByUserId(returnSessionObj(request).getId());
		if(StatusEnum.TYPE2.getValue()!=obj.getStatus()){
			//审核痛过
			model.add("msg",MSG_3);
			return "/views/tb/error.vm";
		}
		
		List<Map> gamelis =  gameDao.queryAllForEndDate();
		model.add("gamelis", gamelis);
		return "/views/tb/recommendationDetail.vm";
	}
	
	@Post("create")
	public String create(Model model,TBRecommendation tBRecommendation,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			
			String gameObj = tBRecommendation.getGameName();
			int gameId = Integer.parseInt(gameObj.split("@")[0]);
			String gameName = gameObj.split("@")[1];
			tBRecommendation.setUserId(returnSessionObj(request).getId());
			tBRecommendation.setGameId(gameId);
			tBRecommendation.setGameName(gameName);
			tBRecommendation.setStatus(StatusEnum.TYPE2.getValue());
			tBRecommendation.setCreateUser(returnSessionObj(request).getAccount());
			tBRecommendation.setUpdateUser(returnSessionObj(request).getAccount());
			tBRecommendationDao.create(tBRecommendation);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("edit")
	public String edit(Model model,@Param("id") int id) {
		List<Map> gamelis =  gameDao.queryAllForEndDate();
		TBRecommendation obj = tBRecommendationDao.queryById(id);
		model.add("obj", obj);
		model.add("gamelis", gamelis);
		return "/views/tb/recommendationEdit.vm";
	}
	
	@Post("update")
	public String update(Model model,TBRecommendation tBRecommendation,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			
			TBRecommendation obj = tBRecommendationDao.queryById(tBRecommendation.getId());
			
			if(StatusEnum.TYPE1.getValue()==obj.getStatus()){
				String gameObj = tBRecommendation.getGameName();
				int gameId = Integer.parseInt(gameObj.split("@")[0]);
				String gameName = gameObj.split("@")[1];
				
				tBRecommendation.setGameId(gameId);
				tBRecommendation.setGameName(gameName);
				tBRecommendation.setStatus(StatusEnum.TYPE2.getValue());
				tBRecommendation.setUpdateUser(returnSessionObj(request).getAccount());
				tBRecommendationDao.update(tBRecommendation);
			}else{
				//A003:状态不符合
				jo.put("returnFlag", Constants.returnObj.returnCode.code_3);
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteById")
	public String deleteById(Model model,@Param("Ids") String Ids) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		for(String id :Ids.split(",")){
			tBRecommendationDao.deleteById(Integer.parseInt(id));
		}
		
		return "@"+jo.toString();
	}
	
	//**************************************************************
	@Post("getItemList")
	public String queryItemList(TBRecommendationItem tBRecommendationItem) {
		//当前页  
		tBRecommendationItem.setStart((tBRecommendationItem.getPage()-1)*tBRecommendationItem.getRows());
		tBRecommendationItem.setLength(99999);
		
		JSONObject jo = new JSONObject();
		
		List<TBRecommendationItem> objs = tBRecommendationItemDAO.query(tBRecommendationItem);
		
		int size = tBRecommendationItemDAO.querySize(tBRecommendationItem);
		jo.put("rows", objs);
		jo.put("total", size);
		return "@"+jo.toString();
	}
	
	@Get("createItemPage")
	public String createItemPage(Model model,@Param("recommendationId") int recommendationId) {
		List<Map> gragelis = grageDao.queryAll();
		
		TBRecommendation obj = tBRecommendationDao.queryById(recommendationId);
		if(obj!=null){
			
			if(obj.getStatus()!=StatusEnum.TYPE2.getValue() 
					&& obj.getStatus()!=StatusEnum.TYPE6.getValue()){
				model.add("msg",MSG_1);
				return "/views/tb/error.vm";
			}
			
			TBRecommendationItem tBRecommendationItem = new TBRecommendationItem();
			tBRecommendationItem.setRecommendationId(recommendationId);
			int sizeNum = tBRecommendationItemDAO.querySize(tBRecommendationItem);
			
			int num = obj.getNum();
			if(num<=sizeNum){
				model.add("msg",MSG_2);
				return "/views/tb/error.vm";
			}
		}
		
		model.add("gragelis",gragelis);
		model.add("recommendationId",recommendationId);
		return "/views/tb/recommendationItemDetail.vm";
	}
	
	@Post("createItem")
	public String createItem(Model model,TBRecommendationItem tBRecommendationItem,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			tBRecommendationItem.setCreateUser(returnSessionObj(request).getAccount());
			tBRecommendationItem.setUpdateUser(returnSessionObj(request).getAccount());
			tBRecommendationItemDAO.create(tBRecommendationItem);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("editItem")
	public String editItem(Model model,@Param("id") int id) {
		TBRecommendationItem objItem = tBRecommendationItemDAO.queryById(id);
		
		TBRecommendation objHeard = tBRecommendationDao.queryById(objItem.getRecommendationId());
		if(objHeard.getStatus()!=StatusEnum.TYPE2.getValue() 
				&& objHeard.getStatus()!=StatusEnum.TYPE6.getValue()){
			//审核痛过
			model.add("msg",MSG_1);
			return "/views/tb/error.vm";
		}
		
		List<Map> gragelis = grageDao.queryAll();
		
		model.add("obj", objItem);
		model.add("gragelis",gragelis);
		return "/views/tb/recommendationItemEdit.vm";
	}
	
	@Post("updateItem")
	public String updateItem(Model model,TBRecommendationItem tBRecommendationItem,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			tBRecommendationItem.setUpdateUser(returnSessionObj(request).getAccount());
			tBRecommendationItemDAO.update(tBRecommendationItem);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	@Get("deleteItemById")
	public String deleteItemById(Model model,@Param("Ids") String Ids) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		TBRecommendationItem objItem = tBRecommendationItemDAO.queryById(Integer.parseInt(Ids.split(",")[0]));
		TBRecommendation objHeard = tBRecommendationDao.queryById(objItem.getRecommendationId());
		if(objHeard.getStatus()==StatusEnum.TYPE5.getValue() 
				|| objHeard.getStatus()==StatusEnum.TYPE4.getValue()){
			//已申报 & 申报通过
			jo.put("returnFlag", Constants.returnObj.returnCode.code_3);
			return "@"+jo.toString();
		}
		
		
		for(String id :Ids.split(",")){
			tBRecommendationItemDAO.deleteById(Integer.parseInt(id));
		}
		
		return "@"+jo.toString();
	}
	
	/**
	 * 申报页面没有人数
	 * @param model
	 * @return
	 */
	@Get("createRecPage")
	public String createRecPage(Model model,HttpServletRequest request) {
		
		TBSchoolRegistration obj = tBSchoolRegistrationDao.queryByUserId(returnSessionObj(request).getId());
		if(StatusEnum.TYPE2.getValue()!=obj.getStatus()){
			//审核痛过
			model.add("msg",MSG_3);
			return "/views/tb/error.vm";
		}
		
		List<Map> gamelis =  gameDao.queryAllForEndDate();
		model.add("gamelis", gamelis);
		return "/views/tb/recommendationRec.vm";
	}
	
	/**
	 * 申报方法没有人数
	 * @param model
	 * @param tBRecommendation
	 * @param request
	 * @return
	 */
	@Post("createRec")
	public String createRec(Model model,TBRecommendation tBRecommendation,HttpServletRequest request) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		try {
			
			String gameObj = tBRecommendation.getGameName();
			int gameId = Integer.parseInt(gameObj.split("@")[0]);
			String gameName = gameObj.split("@")[1];
			
			tBRecommendation.setGameId(gameId);
			tBRecommendation.setGameName(gameName);
			tBRecommendation.setStatus(StatusEnum.TYPE1.getValue());
			tBRecommendation.setCreateUser(returnSessionObj(request).getAccount());
			tBRecommendation.setUpdateUser(returnSessionObj(request).getAccount());
			tBRecommendation.setUserId(returnSessionObj(request).getId());
			tBRecommendationDao.create(tBRecommendation);
		} catch (Exception e) {
			// TODO: handle exception
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
}